using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;


public class RoomManager : MonoBehaviour {

    [SerializeField]
    int m_roomId;

    public int RoomId {  get { return m_roomId; } }

    [SerializeField]
    int m_basisEnnemies = 5;

    [SerializeField]
    int m_maskedEnnemies = 0;

    [SerializeField]
    int m_masterEnnemies = 0;

    [SerializeField]
    int m_maxEnnemy;

    int m_currentEnnemies;

    [SerializeField]
    List<Ennemy> m_initialEnnemies;

    [SerializeField]
    GameObject[] m_spawners;

    Dave m_dave;

    [SerializeField]
    Door m_enterDoor;

    [SerializeField]
    Door m_outDoor;

    [SerializeField]
    GameObject m_PlayerRespawn;

    bool m_clear;

    public bool Clear {  get { return m_clear; } }

    public GameObject RespawnPoint {  get { return m_PlayerRespawn; } }

    bool m_roomActivated;

   int  m_indexSpawner;

    GameObject getMoreFarSpawnLocation()
    {
        /* GameObject s = null;
         float distance = -1;*/


        GameObject s = m_spawners[m_indexSpawner];
        if (m_indexSpawner < m_spawners.Length -1)
            m_indexSpawner++;
        else
            m_indexSpawner = 0;

      /*  for(int i=0; i< m_spawners.Length; i++)
        {
            float d = Vector3.Distance(m_dave.transform.position, m_spawners[i].transform.position);
            if(s == null || d < distance)
            {
                distance = d;
                s = m_spawners[i];
            }
        }*/

        return s;
    }

    void SpawnEnnemy(EnnemyType level)
    {
        GameObject e = null;
        string path = "Ennemies/";

        switch(level)
        {
            case EnnemyType.MinionBasis:
                path +="MinionBasis";
                break;
            case EnnemyType.MinionMasked:
                path += "MinionMasked";
                break;
            case EnnemyType.MinionMaster:
                path += "MinionMaster";
                break;
            default:
                path += "MinionBasis";
                break;
        }

        e = Resources.Load(path) as GameObject;
        GameObject spawner = getMoreFarSpawnLocation();
        e = (GameObject)Instantiate(e, spawner.transform.position, spawner.transform.rotation);
        Ennemy en = e.GetComponent<Ennemy>();
        en.Activated = true;
        en.onEnnemyDefeated.AddListener(ennemyDefeated);
        m_currentEnnemies++;

        if (m_currentEnnemies < m_maxEnnemy)
            trySpawnEnnemy();

    }

    public void ennemyDefeated(Ennemy e)
    {
        e.onEnnemyDefeated.RemoveListener(ennemyDefeated);
        m_currentEnnemies--;

        switch(e.TypeEnnemy)
        {
            case EnnemyType.MinionBasis:
                m_basisEnnemies--;
                break;
            case EnnemyType.MinionMasked:
                m_maskedEnnemies--;
                break;
            case EnnemyType.MinionMaster:
                m_masterEnnemies--;
                break;
            default:
                break;
        }


        if (m_basisEnnemies > 0 || m_maskedEnnemies > 0 || m_masterEnnemies > 0)
            trySpawnEnnemy();
        else
            roomClean();

    }

    void trySpawnEnnemy()
    {
        if (m_basisEnnemies > 0)
            SpawnEnnemy(EnnemyType.MinionBasis);
        else if (m_maskedEnnemies > 0)
            SpawnEnnemy(EnnemyType.MinionMasked);
        else if (m_masterEnnemies > 0)
            SpawnEnnemy(EnnemyType.MinionMaster);
        else
            roomClean();
    }


    void roomClean()
    {
        m_clear = true;
        m_outDoor.open();
        m_dave.endSideEffect();
    }

    void activeRoom()
    {

        if (m_roomActivated)
            return;

        

        m_roomActivated = true;
        GameManager.m_currentRoom = m_roomId;
        GameManager.m_currentRoomManager = this;
        m_dave.fullLife();
        m_enterDoor.close();

    /*    if (m_initialEnnemies != null && m_initialEnnemies.Count != 0)
        {
            m_currentEnnemies = m_initialEnnemies.Count;
            foreach (Ennemy e in m_initialEnnemies)
            {
                //e.SetSpawner(this);
                e.onEnnemyDefeated.AddListener(ennemyDefeated);
                e.Activated = true;
            }
        }*/

        if (m_currentEnnemies < m_maxEnnemy)
            trySpawnEnnemy();
    }

    public void leaveRoom()
    {
        //TODO : Choix des caracteristiques pour la suite
    }

    // Use this for initialization
    void Start()
    {
        m_dave = FindObjectOfType<Dave>();
        m_currentEnnemies = 0;

        //if (m_initialEnnemies != null && m_initialEnnemies.Count != 0)
        //{
        //    foreach (Ennemy e in m_initialEnnemies)
        //        e.gameObject.SetActive
        //        //e.Activated = false;
        //    //StartCoroutine(waitAndInitialize());
        //}
    }

    IEnumerator waitAndInitialize()
    {
        yield return new WaitForEndOfFrame();
        
    
    }

   

    private void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<Dave>() == null && GameManager.m_gameStarted)
            return;

        activeRoom(); 
    }
}
